/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



package helpdesk;

import java.io.IOException;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;


/**
 * A collection of useful functions.
 *
 * @author brian
 */
public class HelpdeskUtil {
    /**
     * Convert ResultSet to a valid XML document and write it to p. Not used
     * anymore.
     *
     * @param p
     * @param rs
     * @throws SQLException
     */
    public static void printXmlResultSet(PrintWriter p, ResultSet rs) throws SQLException
    {
        ResultSetMetaData rsmd = rs.getMetaData();
        String colName;
        int numCols;
        numCols = rsmd.getColumnCount();

        p.println(
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<results>"
                );
        while (rs.next())
        {
            p.println(" <row>");

            for (int i=1;i<=numCols;i++)
            {
                colName = rsmd.getColumnName(i);
                p.print("  <" + colName + ">" + rs.getString(i) + "</" + colName + ">");
            }

            p.println(" </row>");
        }
        p.println("</results>");
    }

    /**
     * Check if user has been authenticated; return true if they have. If not,
     * redirect the user to the login page and return false.
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public static User authenticateUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        HttpSession s = req.getSession();
        User user = (User)s.getAttribute("user");
        if (user == null)
        {
            resp.sendRedirect("login.jsp");
        }

        return user;
    }

    /**
     * Output error as a valid XML document in a tag named 'error' to p. Not
     * currently used.
     *
     * @param p
     * @param error
     */
    public static void printXmlError(PrintWriter p, String error)
    {
        p.println(
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<error>\n" +
                error + "\n" +
                "</error>"
                );
    }

    /**
     * Place an e-mail in the e-mail table to be sent by the e-mail script when
     * it is run.
     *
     * @param recipient
     * @param title
     * @param contents
     * @throws SQLException
     */
    public static void sendEmail(String recipient, String title, String contents) throws SQLException
    {
        Connection conn = null;
        PreparedStatement ps;

        try
        {
            conn = DBManager.getConnection();
            ps = conn.prepareStatement("INSERT INTO emails (recipient, title, message) VALUES (?, ?, ?)");
            ps.setString(1, recipient);
            ps.setString(2, title);
            ps.setString(3, contents);
            ps.executeUpdate();
        }
        finally
        {
            if (conn == null) conn.close();
        }
    }

    /**
     * Output that we succeded as a valid XML document in a tag named 'success'
     * to p. Not currently used.
     *
     * @param p
     * @param error
     */
    public static void printXmlSuccess(PrintWriter p, String message)
    {
        p.println(
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<success>\n" +
                message + "\n" +
                "</success>"
                );
    }
}
